Column

Chart A

Column

Chart B

Chart C

---
title: "Instacart Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    navbar:
      - { icon: fa-home, href: index.html, align: right }
      - { icon: fa-envelope, href: mailto:<yifeixu9836@gmail.com>, align: right }
      - { icon: fa-github, href: "http://github.com/yifeixu0306/", align: right }
      - { icon: fa-linkedin, href: "https://www.linkedin.com/in/yifei-xu-6190b6228/", align: right }
    source: embed
    theme: lumen
---


```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(p8105.datasets)
library(plotly)
```


```{r}
# Read in the data
data("instacart")

instacart = 
  instacart %>% 
  as_tibble(instacart)
```


```{r}
# Clean the dataset
instacart_tidy = 
  instacart %>% 
  janitor::clean_names() %>%
  mutate(
    day = order_dow + 1,
    order_day_of_week = lubridate::wday(day, label = TRUE)
    ) %>%
  select(order_id, user_id, order_day_of_week, order_hour_of_day, days_since_prior_order, product_name, aisle, department) %>%
  drop_na()

# Top 10 popular aisle
aisle_10 = instacart_tidy %>%
  group_by(aisle) %>% 
  summarise(count = n()) %>% 
  mutate(aisle = str_to_title(aisle)) %>%
  arrange(-count) %>% 
  top_n(10) 

aisle_name_10 = aisle_10 %>%
  pull(aisle)
```


Column {data-width=500}
-----------------------------------------------------------------------

### Chart A

```{r}
instacart_tidy %>%
  group_by(order_day_of_week, order_hour_of_day) %>%
  summarize(count = n_distinct(order_id)) %>%
  plot_ly(x = ~order_hour_of_day, y = ~count, type = "scatter", mode = "lines",
          color = ~order_day_of_week, alpha = 0.8) %>%
  layout(title =  "Distributions of Order Time During a Day",
         xaxis = list(title = "Time", range = list(0,24), dtick = 3, 
                      tickvals = c(0, 3, 6, 9, 12, 15, 18, 21, 24),
                      ticktext = c("12am", "3am", "6am", "9am", "12pm", "3pm", "6pm", "9pm", "12am")),
         yaxis = list(title = "Number of Orders"),
         legend = list(traceorder = "reversed"))
```

Column {data-width=500}
-----------------------------------------------------------------------

### Chart B

```{r}
aisle_10 %>% 
  mutate(aisle = fct_reorder(aisle, -count)) %>%
  plot_ly(x = ~aisle, y = ~count, color = ~aisle, type = "bar", alpha = 0.6) %>%
  layout(title = "Number of Items Ordered in Top 10 Popular Aisles",
         xaxis = list(title = "Aisle"), 
         yaxis = list(title = "Number of items ordered"))
```


### Chart C

```{r}
instacart_tidy %>% 
  mutate(aisle = str_to_title(aisle)) %>%
  filter(aisle %in% aisle_name_10) %>%
  left_join(aisle_10, by = "aisle") %>%
  mutate(aisle = fct_reorder(aisle, -count)) %>% 
  plot_ly(x = ~aisle, y = ~days_since_prior_order, type = "box",
          color = ~aisle, alpha = 0.6) %>% 
  layout(title = "Distributions of Order Interval in the Top 10 Popular Aisles",
         xaxis = list(title = "Aisle"),
         yaxis = list(title = "Days Since Prior Order"))
```